﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IrisGenerator.Common.Core;
using IrisGenerator.Common.UI.Controls.Menus;
using IrisGenerator.Common.Core.UI;
using IrisGenerator.Common.Modules;

namespace IrisGenerator.WinGui
{
    public partial class Main : BaseForm
    {

        
        private IrisGenerator.Common.Core.Projects.Project CurrentProject=null;

        public Main():base()
        {
            InitializeComponent();


            ProjectToolStripMenuItem mnu = new ProjectToolStripMenuItem();
            this.fileToolStripMenuItem.DropDownItems.Add(mnu);
            mnu.SubItemClicked += new EventHandler<IrisGenerator.Framework.Events.BaseEventArgs<IrisGenerator.Common.Core.Projects.Project>>(mnu_SubItemClicked);
        }


        void mnu_SubItemClicked(object sender, IrisGenerator.Framework.Events.BaseEventArgs<IrisGenerator.Common.Core.Projects.Project> e)
        {
            if (e.Argument != null)
                this.CurrentProject = e.Argument;

            LoadProjectLeftPanel();
            LoadAssemblies();
        }

        private void LoadAssemblies()
        {

            foreach (ModuleInstance m in this.CurrentProject.ModuleInstanceCollection)
            {
                
                //if (m.Instance==null)
                    m.CreateInstance();
            }
        }

        
        private void LoadProjectLeftPanel()
        {
            if (this.CurrentProject == null)
                return;

            ProjectVerticalMenu ProjectMenu =
                new ProjectVerticalMenu();

            ProjectMenu.ItemClicked += new EventHandler<IrisGenerator.Framework.Events.BaseEventArgs<string>>(ProjectMenu_ItemClicked);

            this.LeftPanel.Controls.Clear();
            this.LeftPanel.Controls.Add(ProjectMenu);
            
        }

        
        void ProjectMenu_ItemClicked(object sender, IrisGenerator.Framework.Events.BaseEventArgs<string> e)
        {
            if (this.CurrentProject == null)
                return;

            if (e.Argument.Equals("Run Analyse"))
            {
                foreach (ModuleInstance module in this.CurrentProject.ModuleInstanceCollection)
                {
                    module.Instance.Analyse();
                }
                MessageBox.Show("Test");
            }
        }

        private void MenuPreferences_Click(object sender, EventArgs e)
        {
            Preferences frmPreferences = new Preferences();
            frmPreferences.ShowDialog();
        }

        private void registerModulesToolStripMenuItem_Click(object sender, EventArgs e)
        {

            int inserted;
            int updated;
            DatabaseRegistering.RegisterModules(Application.StartupPath,out updated,out inserted);
        }

        private void mnuNewProject_Click(object sender, EventArgs e)
        {
            Project.ProjectCreation createProjectForm =
                new IrisGenerator.WinGui.Project.ProjectCreation();

            createProjectForm.ShowDialog();
        }

       
    }
}
